package com.pmes.utils;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtil {
	/**
	 * 页面Form提交表单时返回执行结果信息
	 */
	public static String getResponseMessage(boolean success){
		return "{'success':" + success + "}";
	}
	
	/**
	 * AJAX提交返回结果信息
	 */
	public static String getResponseMessage(boolean success,String message){
		if(message == null || message.trim().length() == 0){
			if(success){
				message = "操作成功！";
			}else{
				message = "操作失败！";
			}
		}
		return "{'success':" + success + ",'message':'" + message + "'}";
	}
	
	public static <T> String objectToJson(Object object, Class<T> cls)throws JsonGenerationException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.registerSubtypes(cls);
		String reqJson = mapper.writeValueAsString(object);
		return reqJson;
	}
	
	public static String jsonOut(Object jsonObject) throws JsonGenerationException, JsonMappingException, IOException{
		ObjectMapper objectMapper = new ObjectMapper();
		String jsonString = objectMapper.writeValueAsString(jsonObject);
		return jsonString;
	}
}
